﻿// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
// Licensed under the LGPL License, Version 3.0. See License.txt in the project root for license information.
// Website: https://admin.blazor.zone

using BlazorAdmin.Interface;
using BlazorAdmin.Web.Shared.Services;

namespace BlazorAdmin.Web.Shared.Utils
{
    /// <summary>
    /// 登录获取默认首页帮助类
    /// </summary>
    [Obsolete]
    public static class LoginHelper
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <param name="returnUrl"></param>
        /// <param name="appId"></param>
        /// <param name="userService"></param>
        /// <param name="dictService"></param>
        /// <returns></returns>
        public static string GetDefaultUrl(BootstrapAppContext context, string? returnUrl, string? appId, IUser userService, IDict dictService)
        {
            if (string.IsNullOrEmpty(returnUrl))
            {
                // 查找 User 设置的默认应用
                var userName = context.CurrentUser?.UserName;
                var defaultAppId = context.AppID;
                var schema = context.BaseUri.Scheme;
                var host = context.BaseUri.Host;
                appId ??= userService.GetAppIdByUserName(userName) ?? defaultAppId;

                if (!string.IsNullOrEmpty(appId))
                {
                    var appUrl = dictService.GetHomeUrlByAppId(appId);
                    if (!string.IsNullOrEmpty(appUrl))
                    {
                        returnUrl = string.Format(appUrl, schema, host).TrimEnd('/');
                    }
                }
            }

            return returnUrl ?? "/Admin/Index";
        }
    }
}
